package com.hanhanxiaochong.zhongxiangchuanmei.view.home.act;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.TechnicianBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.util.MessageEvent;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.adapter.TechnicianAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 技师列表
 */
public class TechnicianActivity extends BaseActivity {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.txt_title)
    TextView txtTitle;

    private String shopId;
    private String projectId;
    private TechnicianAdapter technicianAdapter;
    private String tech_id;
    private String techName;

    public static Intent createIntent(Context context, String s_id, String p_id) {
        Intent intent = new Intent(context, TechnicianActivity.class);
        intent.putExtra("s_id", s_id);
        intent.putExtra("p_id", p_id);
        return intent;
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_technician);
        Intent intent = getIntent();
        shopId = intent.getStringExtra("s_id");
        projectId = intent.getStringExtra("p_id");
    }

    @Override
    protected void initPresenter() {
        RetrofitSource.createApi(HttpPostService.class)
                .tech(shopId, projectId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<TechnicianBean>>>(this) {
                    @Override
                    public void onSuccess(ResponseBase<List<TechnicianBean>> response) {
                        technicianAdapter.setDatas(response.getData());
                    }
                });
    }

    @Override
    protected void init() {
        txtTitle.setText("选择技师");
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        List<TechnicianBean> list = new ArrayList<>();
        technicianAdapter = new TechnicianAdapter(this, R.layout.item_technician, list);
        recyclerview.setAdapter(technicianAdapter);
        technicianAdapter.setmOnItemClickListener((adapter, position) -> {
            TechnicianBean bean = technicianAdapter.getDatas().get(position);
            tech_id = bean.getId();
            techName = bean.getName();
    });
}

    @OnClick({R.id.ll_leftview, R.id.tv_subscribe_now})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.tv_subscribe_now:
                MessageEvent event = new MessageEvent(tech_id);
                event.setFrom("tech");
                event.setTitle(techName);
                EventBus.getDefault().post(event);
                finish();
                break;
        }
    }
}
